package reflect;

import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URISyntaxException;

/**
 * @author DBX
 * @effect 自动调用reflect包中所有类的无参方法
 * @date 2021/09/15/15:34
 */
public class ReflectDemo8 {
    public static void main(String[] args) throws Exception{
        try {
            File dir = new File(AboutPath.class.getResource(".").toURI());
            String path = dir.getAbsolutePath();
            System.out.println(path);
//            获取Test03.class这个目录下中所有class文件
            File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
            for (File sub : subs) {
                String fileName = sub.getName();
                String className = fileName.substring(0, fileName.indexOf("."));
                Class c = Class.forName("reflect."+className);
                Object o = c.newInstance();
                Method[] methods = c.getDeclaredMethods();
                for (Method method : methods) {
                    if (method.getParameterCount()==0&& method.getModifiers()!= Modifier.PRIVATE){
                        System.out.println("执行方法："+ method.getName()+"()");
                        method.invoke(o);
                    }
                }
            }

        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
}
